package controller;

import com.alibaba.fastjson.JSONArray;
import entity.Category;
import entity.Goods;
import util.JdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author gaojie  gao4433@qq.com
 * @Description
 * @date 2022/6/10 1:49
 * @since 1.8
 */
public class GoodsBlockServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json; charset=UTF-8");

        String sql = "SELECT `id`,`title`,`price`,`image`,`comment`  FROM `ec_simple_goods`";
        List<Goods> goods = JdbcUtils.selects(sql, Goods.class);

        System.out.println(goods);

        PrintWriter writer = response.getWriter();
        if (goods != null) {
            String jsonString = JSONArray.toJSONString(goods);
            writer.write(jsonString);
        } else {
            writer.write("null");
        }
    }

}

